package com.scoy.honeymei.activity.mall;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.adapter.ShopMainAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.GoodsBean;
import com.scoy.honeymei.databinding.ActivityGoodsCatelistBinding;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;

import java.util.List;

public class GoodsListActivity extends BaseActivity {

    private GoodsListActivity mContext;
    private int cateId;
    //0 商品列表 ；1 搜索列表
    private int pageType;
    private int pageInt = 1;
    private String orderStr = "";
    private String searchStr = "";
    private int shopId;
    private ShopMainAdapter hAdapter;
    private com.scoy.honeymei.databinding.ActivityGoodsCatelistBinding binding;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityGoodsCatelistBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = GoodsListActivity.this;
        cateId = getIntent().getIntExtra("cateId", 0);
        shopId = getIntent().getIntExtra("shopId", 0);
        pageType = getIntent().getIntExtra("pageType", 0);
        binding.includeTop.titleTv.setText(getIntent().getStringExtra("title"));
        binding.includeTop.backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initNormal();

        if (pageType == 0) {
            getData(1);
        } else {
            binding.fmmArrLlt.setVisibility(View.GONE);
        }


    }

    @Override
    public void initNormal() {

        MyUtil.setStatusBar(mContext, getWindow(), true, 0);

        binding.includeTop.titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        binding.hmLlt.setVisibility(pageType == 1 ? View.VISIBLE : View.GONE);

        initRv();

        initSearch();

        binding.norSrl.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            hAdapter.clearData();
            getData(1);

        }).setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore(1500);
            getData(pageInt);
        });


        orderStr = "price";
        binding.fmmCheckpriceTv.setSelected(true);
        binding.fmmCheckpriceView.setSelected(true);
        binding.fmmCheckstarTv.setSelected(false);
        binding.fmmCheckstarView.setSelected(false);

        binding.fmmCheckpriceTv.setOnClickListener(v -> {
            if ("price".equals(orderStr)) return;
            orderStr = "price";
            binding.fmmCheckpriceTv.setSelected(true);
            binding.fmmCheckpriceView.setSelected(true);
            binding.fmmCheckstarTv.setSelected(false);
            binding.fmmCheckstarView.setSelected(false);
            binding.norSrl.autoRefresh();

        });
        binding.fmmCheckstarTv.setOnClickListener(v -> {
            if ("star".equals(orderStr)) return;
            orderStr = "star";
            binding.fmmCheckpriceTv.setSelected(false);
            binding.fmmCheckpriceView.setSelected(false);
            binding.fmmCheckstarTv.setSelected(true);
            binding.fmmCheckstarView.setSelected(true);
            binding.norSrl.autoRefresh();
        });

    }


    private void initSearch() {
        binding.hmSearchTv.setOnClickListener(v -> {
            if (!MyUtil.isFastClick()) return;
            if (TextUtils.isEmpty(binding.hmSearchEt.getText())) {
                searchStr = "";
            } else {
                if (binding.fmmArrLlt.getVisibility() == View.GONE)
                    binding.fmmArrLlt.setVisibility(View.VISIBLE);
                searchStr = binding.hmSearchEt.getText().toString().trim();
                hAdapter.clearData();
                getData(1);
            }
        });

        binding.hmSearchTv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (!MyUtil.isFastClick()) return false;
                if (TextUtils.isEmpty(binding.hmSearchEt.getText())) {
                    searchStr = "";
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    searchStr = binding.hmSearchEt.getText().toString().trim();
                    if (binding.fmmArrLlt.getVisibility() == View.GONE)
                        binding.fmmArrLlt.setVisibility(View.VISIBLE);
                    hAdapter.clearData();
                    getData(1);
                }
                return false;
            }
        });


    }


    private void initRv() {
        hAdapter = new ShopMainAdapter(mContext);
        GridLayoutManager glm = new GridLayoutManager(mContext, 2, RecyclerView.VERTICAL, false);
        binding.fmmGoodsRv.setLayoutManager(glm);
        binding.fmmGoodsRv.setAdapter(hAdapter);
        hAdapter.setOnOneClick(pos -> {
            //to goods
            GoodsBean item = hAdapter.getItem(pos);
            int id = item.getId();
            Intent intentde = new Intent(mContext, GoodsDetailActivity.class);
            intentde.putExtra("goodsId", id + "");
            startActivityForResult(intentde, 0x871);
        });


    }

    private void getData(int myPage) {
        if (myPage == -1) {
            MyUtil.mytoast(mContext, "没有更多了~");
            return;
        }
        HttpParams params = new HttpParams();
        params.put("shop_id", shopId);
        params.put("order", orderStr);
        params.put("cate_id", cateId);
        params.put("page", myPage);
        params.put("keywords", searchStr);
        HpGo.newInstance().HttpGo(mContext, MyUrl.SHOP_GOODS_LIST, params, new ApiCallBack() {
            @Override
            public void onSuccess(String body) {
                List<GoodsBean> mlist = new Gson().fromJson(body, new TypeToken<List<GoodsBean>>() {
                }.getType());

                hAdapter.addData(mlist);
                pageInt = mlist.size() == 0 ? -1 : myPage + 1;
                binding.nodataTv.setVisibility(hAdapter.getData().size() == 0 ? View.VISIBLE : View.GONE);
            }
        });
    }


}
